local tianji = fk.CreateSkill{
    name = "XD__tianji",
}
Fk:loadTranslationTable{
    ["XD__tianji"] = "天极",
    [":XD__tianji"] = "牌堆顶的牌对你可见，你可以使用或打出牌堆顶的红色牌；你以此法使用【杀】或【决斗】时，你视为拥有技能“无双”。",
    ["@[XD__tianji]-phase"] = "天极",
}

local function getNumberStr(num)
    if num == 1 then
        return "A"
    elseif num == 11 then
        return "J"
    elseif num == 12 then
        return "Q"
    elseif num == 13 then
        return "K"
    elseif num == 0 then
        return "X"
    end
    return tostring(num)
end

--- 获取卡牌的缩略文字信息。
local function toLogString(cardID)
    local self = Fk:getCardById(cardID)
    if not self then return " " end
    local cardName = Fk:translate(self.name)
    if #cardName > 9 then
        cardName = string.sub(cardName, 1, 6)
    end
    local ret = string.format('<font color="#0598BC"><b>%s</b></font>', cardName)
    if self:isVirtual() and #self.subcards ~= 1 then
        ret = ret .. Fk:translate(self:getColorString())
    else
        ret = ret .. Fk:translate("log_" .. self:getSuitString())
        if self.number > 0 then
        ret = ret .. string.format('<font color="%s"><b>%s</b></font>', self.color == Card.Red and "#CC3131" or "white", getNumberStr(self.number))
        end
    end
    ret = ret .. '<font color="#0598BC"></font>'
    return ret
end

Fk:addQmlMark{
    name = "XD__tianji",
    how_to_show = function (name, value, player)
        if type(value) ~= "table" then return " " end
        if (value.players == nil and Self == p) or (value.players and table.contains(value.players, Self.id)) then
            if type(value.value) ~= "table" or #value.value == 0 or type(value.value[1]) ~= "number" or not Fk:getCardById(value.value[1]) then return " " end
            local str = toLogString(value.value[1])
            return str
        else
            return " "
        end
    end,
    qml_data = function (name, value, player)
        return value.value
    end,
    qml_path = function(name, value, p)
        if type(value) ~= "table" then return " " end
        if (value.players == nil and Self == p) or (value.players and table.contains(value.players, Self.id)) then
            if type(value.value) ~= "table" or #value.value == 0 or type(value.value[1]) ~= "number" or not Fk:getCardById(value.value[1]) then return " " end
            return "packages/utility/qml/ViewPile"
        else
            return " "
        end
    end,
}
tianji:addEffect("filter",{
    handly_cards = function (self, player)
        if player:hasSkill(tianji.name) then
            if #Fk:currentRoom().draw_pile > 0 then
                if Fk:getCardById(Fk:currentRoom().draw_pile[1]).color == Card.Red then
                    return {Fk:currentRoom().draw_pile[1]}
                end
            elseif #player:getTableMark("@[XD__tianji]-phase").value > 0 then
                if Fk:getCardById(player:getTableMark("@[XD__tianji]-phase").value[1]).color == Card.Red then
                    return player:getTableMark("@[XD__tianji]-phase").value
                end
            end
        end
    end,
})
tianji:addEffect(fk.PreCardUse,{
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(tianji.name) and target == player and #table.filter(Card:getIdList(data.card),function (id)
            return table.contains(player:getTableMark("@[XD__tianji]-phase").value,id)
        end) > 0
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player,"XD__tianji","special")
        data.extra_data = data.extra_data or {}
        data.extra_data.XD__tianji = true
        --player:broadcastSkillInvoke("XD__tianji")
        if #room.draw_pile > 0 then
            player.room:setPlayerMark(player, "@[XD__tianji]-phase", {value = {room.draw_pile[1]}, players = {player.id}})
        end
    end,
})
local spec = {
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(tianji.name) and #Fk:currentRoom().draw_pile > 0
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        player.room:setPlayerMark(player, "@[XD__tianji]-phase", {value = {room.draw_pile[1]}, players = {player.id}})
    end,
}
tianji:addEffect(fk.AskForCardUse,spec)
tianji:addEffect(fk.AskForCardResponse,spec)
tianji:addEffect(fk.AfterCardsMove,spec)
tianji:addEffect(fk.AfterDrawPileShuffle,spec)
tianji:addEffect(fk.EventPhaseStart,spec)
tianji:addAcquireEffect(function (self, player, is_start)
    player.room:addSkill("XD__wushuang")
    if #player.room.draw_pile > 0 then
        player.room:setPlayerMark(player, "@[XD__tianji]-phase", {value = {player.room.draw_pile[1]}, players = {player.id}})
    end
end)
tianji:addLoseEffect(function (self, player, is_death)
    player.room:setPlayerMark(player, "@[XD__tianji]-phase", 0)
end)

return tianji